package org.opens.converter.sqlType.handle;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.opens.converter.sqlType.I_SqlTypeConverter;
import org.opens.converter.sqlType.SqlTypeConverterEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述
 *
 * @author by zhangyuming
 * @date 2023/3/7
 */
public class MysqlSqlTypeConverterHandle implements I_SqlTypeConverter {

    private static Log log = LogFactory.get(MysqlSqlTypeConverterHandle.class);

    private static Map<String, SqlTypeConverterEntity> converterMap = new HashMap<>();

    static {
        converterMap.put("bit", SqlTypeConverterEntity.builder().javaTypeClassName("Boolean").javaTypeFullName("java.lang.Boolean").build());
        converterMap.put("boolean", SqlTypeConverterEntity.builder().javaTypeClassName("Boolean").javaTypeFullName("java.lang.Boolean").build());
        converterMap.put("tinyint", SqlTypeConverterEntity.builder().javaTypeClassName("Byte").javaTypeFullName("java.lang.Byte").build());
        converterMap.put("smallint", SqlTypeConverterEntity.builder().javaTypeClassName("Short").javaTypeFullName("java.lang.Short").build());
        converterMap.put("int", SqlTypeConverterEntity.builder().javaTypeClassName("Integer").javaTypeFullName("java.lang.Integer").build());
        converterMap.put("bigint", SqlTypeConverterEntity.builder().javaTypeClassName("Long").javaTypeFullName("java.lang.Long").build());
        converterMap.put("float", SqlTypeConverterEntity.builder().javaTypeClassName("Float").javaTypeFullName("java.lang.Float").build());
        converterMap.put("double", SqlTypeConverterEntity.builder().javaTypeClassName("Double").javaTypeFullName("java.lang.Double").build());
        converterMap.put("decimal", SqlTypeConverterEntity.builder().javaTypeClassName("BigDecimal").javaTypeFullName("java.math.BigDecimal").build());
        converterMap.put("varchar", SqlTypeConverterEntity.builder().javaTypeClassName("String").javaTypeFullName("java.lang.String").build());
        converterMap.put("char", SqlTypeConverterEntity.builder().javaTypeClassName("Byte").javaTypeFullName("java.lang.Byte").build());
        converterMap.put("datetime", SqlTypeConverterEntity.builder().javaTypeClassName("Date").javaTypeFullName("java.util.Date").build());
        converterMap.put("blob", SqlTypeConverterEntity.builder().javaTypeClassName("Byte[]").javaTypeFullName("java.util.Byte").build());
        converterMap.put("jsonb", SqlTypeConverterEntity.builder().javaTypeClassName("Map<String, Object>").javaTypeFullName("java.util.Map").build());
    }

    @Override
    public SqlTypeConverterEntity dbType2JavaType(String dbSqlType) {
        SqlTypeConverterEntity converterEntity = converterMap.get(dbSqlType);
        if (converterEntity == null) {
            log.info("未配置的sqlType类型: {}, 映射至默认的Object类型!", dbSqlType);
            return SqlTypeConverterEntity.getDefault();
        }
        return converterMap.get(dbSqlType);
    }
}
